package com.nondo.rdp.finance.endpoint.api.app.user;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.interceptor.OperationLog;
import com.nondo.rdp.auth.model.UserBeanInner;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.UserAuth;
import com.nondo.rdp.finance.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController("apiAppUserHome")
@RequestMapping("/api/app/user")
public class HomeEndpoint {
    @Autowired
    UserService userService;
    @Autowired
    UserAuthService userAuthService;

    /**
     * 获取用户信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/home")
    public View findOne(HttpServletRequest request, HttpServletResponse response) {
        View view = new View(20000, "获取成功");
        User user = UserUtils.getUser(request, response);

        Map map = new HashMap();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("nickName", user.getNickName());
        map.put("avatar", user.getAvatar());
        if (user.getSign() != null && user.getSign().length() > 0) {
            map.put("sign", user.getSign());
        } else {
            map.put("sign", "这个人很懒，什么都没有留下");
        }

        //查询认证信息
        String authHeader = "";
        Optional<UserAuth> userAuthIsExist = this.userAuthService.findByUser(user);
        if (userAuthIsExist.isPresent()) {
            UserAuth userAuth = userAuthIsExist.get();
            String authString = "";
            if (userAuth.getIdentityAuth() == UserAuth.PROGRESS_PASS) {
                authString = "实名认证";
            }
            if (userAuth.getMasterAuth() == UserAuth.PROGRESS_PASS) {
                authString = "达人认证";
                authHeader = "达人认证";
            }
            if (userAuth.getAdviserAuth() == UserAuth.PROGRESS_PASS) {
                authString = "投顾认证";
                authHeader = "投顾认证";
            }
            authString = (authString.length() > 0) ? authString : "未认证";
            map.put("auth", authString);
        } else {
            map.put("auth", "未认证");
        }

        map.put("authHeader", authHeader);

        view.setData(map);

        return view;
    }

    /**
     * 修改用户信息
     *
     * @param type
     * @param userRequest
     * @param request
     * @param response
     * @return
     */
    @OperationLog
    @RequestMapping(value = "/update/{type}")
    public View update(@PathVariable("type") String type, @RequestBody UserBeanInner userRequest, HttpServletRequest request, HttpServletResponse response) {
        User user = UserUtils.getUser(request, response);

        //根据不同的类型处理操作
        switch (type) {
            case "avatar":
                if (userRequest.getAvatar() == null) {
                    return View.ofError("请传递参数");
                }
                user.setAvatar(userRequest.getAvatar());
                this.userService.save(user);
                break;
            case "nickname":
                if (userRequest.getNickName() == null) {
                    return View.ofError("请传递参数");
                }
                if (userRequest.getNickName().length() > 10) {
                    return View.ofError("用户名不能超过10个字");
                }
                if (userRequest.getNickName().length() < 2) {
                    return View.ofError("用户名不能少于2个字");
                }

                boolean flg = true;
                Pattern p = Pattern.compile("[A-Za-z0-9\\u4e00-\\u9fa5]{2,10}");
                Matcher m = p.matcher(userRequest.getNickName());
                flg = m.matches();
                if (flg == false) {
                    return View.ofError("昵称只能使用中文、英文，不允许特殊字符");
                }

                user.setNickName(userRequest.getNickName());
                this.userService.save(user);
                break;
            case "sign":
                if (userRequest.getSign() == null) {
                    return View.ofError("请传递参数");
                }
                user.setSign(userRequest.getSign());
                this.userService.save(user);
                break;
            default:
                return View.ofError("不存在的类型");
        }

        return View.ofOk("修改成功");
    }
}